/*
 * 使用 express.Router 类创建  模块化、可挂载  的路由句柄
 * @author li.xiaomei
 * @date 2016-12-19
 */

var express = require('express');
var fs = require('fs')
//Router实例是一个完整的中间件和路由系统
var router = express.Router();
var request = require('request')

var dirUtil = require('../util/showFolderFile');
var proxyApi = require('./proxy');

//该路由使用的中间件
// router.use(function timeLog(req, res, next){
//   console.log('Time: '+ Date.now());
//
//   next();
// });

//定义中间件
router.use('/**', function(req, res, next){
  var url = req.originalUrl,
      urlReg = global.think_config.mock.urlRule;
  if(urlReg.test(url)) {
    proxyApi(req, res, next);
  }else {

    if(url=='/'){
      res.send(dirUtil.showFileNames());
    }else {
      url = (url).slice(0,url.length-1);
      res.send(dirUtil.showFileNames(url));
    }
  }
});

module.exports = router;
